package com.ics.sapecit.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.ics.sapecit.bean.Column;
import com.ics.sapecit.bean.Table;
import com.ics.sapecit.dao.ISystemDao;

public class SystemDaoImpl extends JdbcTemplate implements ISystemDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.jdbc.support.JdbcAccessor#setDataSource(javax.sql
	 * .DataSource)
	 */
	@Override
	public void setDataSource(DataSource dataSource) {
		// TODO Auto-generated method stub
		super.setDataSource(dataSource);
	}

	@SuppressWarnings("unchecked")
	public List<Table> getUserTables() {
		String sql = "select table_name from user_tables";
		return this.query(sql, new RowMapper() {

			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				Table table = new Table();
				table.setTableName(rs.getString("TABLE_NAME"));
				return table;
			}
		});
	}

	@SuppressWarnings("unchecked")
	public List<Column> getUserTablesColumns(Table table) {
		String sql = "select * from user_tab_columns where Table_Name=?";
		return this.query(sql, new Object[]{table.getTableName()}, new RowMapper() {

			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				Column column = new Column();
				column.setTableName(rs.getString("TABLE_NAME"));
				column.setColumnName(rs.getString("COLUMN_NAME"));
				column.setDataType(rs.getString("DATA_TYPE"));
				return column;
			}
		});
	}

}
